package com.jgp.glmeeting.controller;


import com.jgp.glmeeting.model.Inform;
import com.jgp.glmeeting.model.Meeting;
import com.jgp.glmeeting.model.Participants;
import com.jgp.glmeeting.service.InformService;
import com.jgp.glmeeting.service.MeetingService;
import com.jgp.glmeeting.service.ParticipantsService;
import com.jgp.sys.common.controller.JGPController;
import com.jgp.sys.ui.GridResult;
import com.jgp.sys.ui.Pager;
import com.jgp.sys.ui.Result;
import com.jgp.sys.ui.annotation.UIParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/glMeeting/informApiController")
public class InformApiController extends JGPController {

    private final Logger logger = LoggerFactory.getLogger(InformApiController.class);

    @Autowired
    private InformService informService;
    @Autowired
    private MeetingService meetingService;
    @Autowired
    private ParticipantsService participantsService;

    @RequestMapping("/queryInforms")
    public Result queryInforms(Inform i, @UIParam("pager") Pager pager){
        List<Inform> informs=informService.queryInforms(i,pager);
        List<GridResult.Category> categories = new ArrayList<>();
        categories.add(new GridResult.Category(  "通知标题",  "informTitle"));
        categories.add(new GridResult.Category(  "会议名称",  "meetingID",Meeting.class,"name"));
        categories.add(new GridResult.Category("创建时间",  "createDatetime"));
        return ajaxRe("gdata",categories,informs,pager);
    }








    //添加通知__恶心
    @RequestMapping("/addInform")
    public Result addInform(String[] usersIDs,String informTitle,String meetingID,String informContent){
        if(StringUtils.isBlank(informTitle) || StringUtils.isBlank(meetingID) || StringUtils.isBlank(informContent) || usersIDs == null || usersIDs.length == 0) {
            return new Result(false);
        }
        Meeting meeting = meetingService.queryID(meetingID);
        if(meeting == null) {
            return new Result(false);
        }
        for(String participantId : usersIDs) {
            Participants participants = participantsService.queryById(participantId);
            if(participants == null) {
                logger.error("参会人员被删除，participantId={}", participantId);
                continue;
            }

            Inform inform = new Inform();
            inform.setInformContent(informContent);
            inform.setInformTitle(informTitle);
            inform.setMeetingID(meetingID);
            inform.setParticipantsId(participantId);
            //添加通知并发送短信
            try {
                informService.addInformAndSendSms(inform, participants, meeting);
            } catch (Exception e) {
                logger.error("发送短信失败,participantsId={}, meetingId={}, informContent={}",participantId,meetingID, informContent);
            }
        }
        return ajaxRe(true);
    }

}
